package org.distributed.transaction.core.interceptor;

import java.sql.Connection;

import org.distributed.transaction.core.callback.ConnectionCallback;
import org.distributed.transaction.core.connection.TransactionConnectionProxy;
import org.distributed.transaction.core.context.TransactionConnectionProxyContext;
import org.distributed.transaction.core.context.TransactionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DataSourceInterceptor {

	@Autowired
	private TransactionConnectionProxyContext transactionConnectionProxyContext;

	public Connection getConnection(ConnectionCallback connectionCallback) throws Throwable {
		TransactionContext transactionContext = TransactionContext.get();

		/**
		 * 根据不同的事务类型，获取不同的获取代理连接的执行策略
		 */
		TransactionConnectionProxy transactionConnectionProxy = transactionConnectionProxyContext
				.getTransactionConnectionProxy(transactionContext.getDtTransactionType());
		return transactionConnectionProxy.getConnection(connectionCallback);
	}
}
